package com.common.www.controller.project;

import com.alibaba.fastjson.JSON;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.project.*;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.project.*;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;



@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/projectVersion"})
@Api(tags = "MGT 公司管理接口")
public class MGTProjectVersionController extends DefaultCrudController<ProjectVersion, Long> {
    @Autowired
    private ProjectVersionService service;
    @Autowired
    private ProjectVersionNotesService versionNotesService;


    protected IBaseService getService() {
        return service;
    }
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        ProjectVersion parsedProject = JSON.parseObject(json, ProjectVersion.class);
        if (parsedProject.getId() == null) {
            service.saveAndFlush(parsedProject);
        } else {
            // 如果 id 不为空，则从数据库中获取现有对象
            ProjectVersion existingProject = service.getByIdLogic(parsedProject.getId());
            if (existingProject != null) {
                // 如果存在，则更新现有对象的属性
                BeanUtils.copyProperties(parsedProject, existingProject, "id");
                // 然后保存更新后的对象
                service.saveAndFlush(existingProject);
            } else {
                // 如果不存在，则保存解析出来的对象
                service.saveAndFlush(parsedProject);
            }
        }
        //查询该项目是否已创建ProjectVersionNotes的内容
        ProjectVersionNotes versionNotes = versionNotesService.getRepository().findByFlagAndProjectId(1,parsedProject.getProject().getId());
        if (versionNotes == null){
            ProjectVersionNotes projectVersionNotes = new ProjectVersionNotes();
            projectVersionNotes.setProject(parsedProject.getProject());
            versionNotesService.save(projectVersionNotes);
        }
        return R.ok(parsedProject);
    }


    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "operate", value = "1.删除 2.启用", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids, Integer operate) {
        if (ParamsUtils.isInputEmpty(ids, operate)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            ProjectVersion project = service.findOne(ids[i]);
            if(project!=null){
                project.setFlag(0);
                service.save(project);
            }
        }
        return R.ok();
    }




}

